package com.zyj.test.month1.day11;

import com.zyj.test.ListNode;

/**
 * @author Yijia Zhang
 * @create 2024-05-18 11:55
 */
public class code0021 {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode dummy = new ListNode(-1);
        ListNode p = dummy;
        while (list1 != null && list2 != null) {
            int v1 = list1.val;
            int v2 = list2.val;
            if (v1 <= v2) {
                ListNode n = list1.next;
                list1.next = null;
                p.next = list1;
                list1 = n;
            } else {
                ListNode n = list2.next;
                list2.next = null;
                p.next = list2;
                list2 = n;
            }
            p = p.next;
        }
        if (list1 != null) {
            p.next = list1;

        } else {
            p.next = list2;
        }

        return dummy.next;
    }
}
